package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.dishMapper;
import com.sky.mapper.setMealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private setMealMapper setmealMapper;
    @Autowired
    private dishMapper dishMapper;

    @Override
    public PageResult categoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.categoryPage(categoryPageQueryDTO);
        long l = page.getTotal();
        List<Category> list = page.getResult();
        return new PageResult(l, list);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        //根据传过来的分类名称查出整条分类信息
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //LocalDateTime now = LocalDateTime.now();
        //修改更新时间
        //category.setUpdateTime(now);
        //category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.setCategoryInfo(category);
    }

    @Override
    public void setStatus(String status, String id) {
        Category category = Category.builder()
                .id(Integer.toUnsignedLong(Integer.parseInt(id)))
                .status(Integer.parseInt(status))
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.setCategoryInfo(category);
    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //LocalDateTime now = LocalDateTime.now();
        //修改更新时间
       /* category.setUpdateTime(now);
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateTime(now);*/
        //新添加的分类，初始状态设为停售
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.addCategory(category);
    }

    @Override
    public void deleCategory(String id) {
        Integer ID = Integer.parseInt(id);
        long count = setmealMapper.cheakById(ID);
        if (count > 0) {
            throw new DeletionNotAllowedException("该分类id关联了套餐表，删除失败");
        }
        count = dishMapper.cheakById(ID);
        if (count > 0) {
            throw new DeletionNotAllowedException("该分类id关联了菜品表，删除失败");
        }
        categoryMapper.deleteById(ID);
    }

    @Override
    public List<Category> select(String type) {
        Integer type1=Integer.parseInt(type);
        List<Category> select = categoryMapper.select(type);
        return  select;
    }
}
